<template>
  <!-- 布局容器 -->
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div class="logo-title">
        <img src="../assets/heima_logo.png" alt="">
        <h2>电商后台管理系统</h2>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 下面主体区域 -->
    <el-container>
      <!-- 左侧menu菜单 -->
      <el-aside :width="iscollapse ? '65px' : '200px'">
        <div class="toggleBar" @click="iscollapse=!iscollapse">|||</div>
        <el-menu
          default-active="2"
          class="el-menu-vertical-demo"
          background-color="#333744"
          text-color="#fff"
          router
          :collapse="iscollapse"
          :collapse-transition="false"
          active-text-color="#409EFF"
          unique-opened>
            <el-submenu :index="item.id + ''" v-for="(item,i) in menus" :key="item.id" :width="iscollapse ? '65px' : '200px'">
              <template slot="title">
                <i :class="['iconfont',iconlist[i]]"></i>
                <span>{{item.authName}}</span>
              </template>
              <!-- 循环创建二级菜单 :index 开启路由 -->
              <el-menu-item :index="'/' + subitem.path" v-for="subitem in item.children" :key="subitem.id"><i class="el-icon-menu"></i>{{subitem.authName}}</el-menu-item>
            </el-submenu>
          </el-menu>
      </el-aside>
      <!-- 右侧主体部分 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      menus: [],
      // 一级菜单对应的字体图标
      iconlist: ['icon-users', 'icon-tijikongjian', 'icon-shangpin', 'icon-danju', 'icon-baobiao'],
      // 是否被折叠,默认为false
      iscollapse: false
    }
  },
  created() {
    // 获取左侧列表数据
    this.getmenus()
  },
  methods: {
    // 退出登录
    logout() {
      // 清空token
      window.sessionStorage.removeItem('token')
      // 跳转到登录页
      this.$router.push('/login')
    },
    // 菜单列表
    async getmenus() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error('获取左侧菜单列表失败!')
      // 将数据保存到data中
      this.menus = res.data
    }
  }
}
</script>
<style lang="less" scoped>
.home-container {
  height: 100%;
  .el-header {
    background-color: #373d41;
    user-select: none;
    padding: 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .logo-title {
      display: flex;
      align-items: center;
      color: #fff;
      h2 {
        font-weight: 200;
        margin-left: 15px;
      }
    }
    .el-button {
      margin-right: 10px;
    }
  }
  .el-aside {
    user-select: none;
    background-color: #333744;
    .iconfont {
      margin-right: 8px;
    }
    .toggleBar {
      background-color: #4a5064;
      color: #fff;
      font-size: 12px;
      line-height: 24px;
      text-align: center;
      letter-spacing: 0.2em;
      cursor:pointer;
    }
  }
  .el-main {
    background-color: #eaedf1;
  }
}
</style>
